<?php

class AuthController extends AppController
{				
	var $name = 'Auth';
	var $helpers = array('Ajax','Time','Pagination');
	var $components = array('Email');
	var $uses = array('CompanyAccount');
	var $layout = 'default';
	
//	var $access = array ('login'=>array('System','Admin','Staff'),
//						);
			
	/**
	* @author yangjie
	* @desc 管理员登陆
	**/			
	function login()
	{
	   	$this->layout = 'ajax'; 
	   			
		//set the redirect for the form
		$redirect = (!empty($this->params['pass'])) ? join('/',$this->params['pass']) : $this->startpage;//set redirect path
		$this->set('redirect',$redirect);
		
		//RENDER VIEW IF USER IS LOGGED IN
		if($this->rdAuth->id && $this->rdAuth->role)
		{
			$this->redirect('top/index');
		}

		//RENDER LOGIN FORM AND THEN HANDLE POST
		if (empty($this->params['data']))
		{
			$this->render('login');
			exit;
		}
		
		//LOGIN FORM VALID			
		$message = $this->CompanyAccount->Login_Valid($this->Valid,$this->params['data']);
		
		if($message){
			$this->set('message', $message);
			$this->params['data']['CompanyAccount']['login_pwd'] = '';
			$this->render('login', $this->layout);
			exit;
		}


		//LOGIN ID CHECK
		$check_data = $this->CompanyAccount->find("login_id = '".$this->params['data']['CompanyAccount']['login_id']."'");
		if(!$check_data['CompanyAccount']['id']){
			$this->set('message', $this->Valid->tagErrorMsg('incorrect','CompanyAccount/login_id'));
			$this->params['data']['CompanyAccount']['login_pwd'] = '';
			$this->render('login', $this->layout);
			exit;
		}
		
		//PASSWORD CHECK
// 		$login_pwd = md5($this->params['data']['CompanyAccount']['login_pwd']);
// 		$condition = "login_id = '".$this->params['data']['CompanyAccount']['login_id']."'" .
// 				     " AND login_pwd = '".$login_pwd."' AND deleted is null";
// 		$check_data = $this->CompanyAccount->find($condition);
		
		if ($check_data['CompanyAccount']['id'])
		{
			
			//sets up the session vars
			$this->rdAuth->set($check_data['CompanyAccount']);
			$this->log('Auth '.$this->params['data']['CompanyAccount']['login_id'].' Logined!',LOG_DEBUG);
			$this->params['data'] = array();
			$this->redirect("http://".$_SERVER['SERVER_NAME'].":82/".$redirect);//验证成功，页面跳转
		}
		else
		{
			$this->set('message', $this->Valid->tagErrorMsg('incorrect','CompanyAccount/login_pwd'));
			$this->params['data']['CompanyAccount']['login_pwd'] = '';
			$this->render('login', $layout);
			exit;
		}		
	}
	
	function download(){
		$data = $this->CompanyAccount->findAll();
		$file_name = date("YmdHis")."_auth" ;
		$col_name = array('CompanyAccount/id'=>'编号',
						  'CompanyAccount/company_id'=>'公司编号',
						  'CompanyAccount/login_id'=>'LoginID'
						);
		$this->Excel->createExcel($file_name,$col_name,$data);
		exit;
	}
	
	
	
}

?>